﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using FOModels.Partner;
using Bussiness.Partner;
using Bussiness.Menu;
using FOModels.Menu;
using Bussiness.SlideWin;
using FOModels.SlideWin;
using FOModels.Language;
using Bussiness.Language;
using FOSystem.CultureHelper;
using FOModels.PHighLight;
using Bussiness.PHighLight;
using Bussiness.Company;
using FOModels.Company;
using System.Net.Mail;
using System.Net.Mime;
using System.Configuration;
namespace FO.Controllers
{
    public class PartialViewController : CultureController
    {
        //
        // GET: /PartialView/

        public ActionResult Index()
        {
            return View();
        }
        public ActionResult gettopmenu()
        {
            MenuBLL bll = new MenuBLL();

            int TopMenuTypeID = int.Parse(System.Configuration.ConfigurationManager.AppSettings["TopMenuTypeID"].ToString());
            LanguageBLL langbll = new LanguageBLL();
            Language lang = langbll.getLangByCode(Request.Cookies["_culture"]["lang"]);
            List<Menu> list = new List<Menu>();
            list = bll.getmenubytypeid(TopMenuTypeID, lang.LangID);
            return PartialView(list);
        }
        public ActionResult getbottommenu()
        {
            MenuBLL bll = new MenuBLL();
            LanguageBLL langbll = new LanguageBLL();
            Language lang = langbll.getLangByCode(Request.Cookies["_culture"]["lang"]);
            int BottomMenuTypeID = int.Parse(System.Configuration.ConfigurationManager.AppSettings["BottomMenuTypeID"].ToString());
            List<FO.ViewModels.MenuGroup> listmenu = new List<ViewModels.MenuGroup>();
            List<Menu> list = new List<Menu>();
            list = bll.getmenubytypeid(BottomMenuTypeID, lang.LangID);
            foreach (var menu in list)
            {
                FO.ViewModels.MenuGroup mng = new ViewModels.MenuGroup();
                mng.menuid = menu.MenuID;
                mng.menuname = menu.Name;
                mng.subMenus = bll.getmenubyparentid(menu.MenuID, lang.LangID);
                listmenu.Add(mng);
            }
            return PartialView(listmenu);
        }
        public ActionResult MenuBottomXML(int id)
        {
            Response.ContentType = "text/xml";
            MenuBLL bll = new MenuBLL();
            LanguageBLL langbll = new LanguageBLL();
            Language lang = langbll.getLangByCode(Request.Cookies["_culture"]["lang"]);
            int BottomMenuTypeID = int.Parse(System.Configuration.ConfigurationManager.AppSettings["BottomMenuTypeID"].ToString());
            List<FO.ViewModels.MenuGroup> listmenu = new List<ViewModels.MenuGroup>();
            List<Menu> list = new List<Menu>();
            list = bll.getmenubytypeid(BottomMenuTypeID, id);
            foreach (var menu in list)
            {
                FO.ViewModels.MenuGroup mng = new ViewModels.MenuGroup();
                mng.menuid = menu.MenuID;
                mng.menuname = menu.Name;
                mng.subMenus = bll.getmenubyparentid(menu.MenuID, id);
                listmenu.Add(mng);
            }
            return View(listmenu);
        }

        public ActionResult getmainmenu()
        {
            LanguageBLL langbll = new LanguageBLL();
            Language lang = langbll.getLangByCode(Request.Cookies["_culture"]["lang"]);
            MenuBLL bll = new MenuBLL();
            List<FO.ViewModels.MainMenuGroup> listmenu = new List<ViewModels.MainMenuGroup>();
            List<MainMenu> list = new List<MainMenu>();
            list = bll.getmainmenu(lang.LangID);
            foreach (var menu in list)
            {
                FO.ViewModels.MainMenuGroup mng = new ViewModels.MainMenuGroup();
                mng.menuid = menu.MenuID;
                mng.menuname = menu.Name;
                mng.links = menu.Links;
                mng.target = menu.Target;
                mng.rel = menu.Rel;
                mng.ulclass = menu.ULClass;
                mng._class = menu.Class;
                mng.id = menu.ID;
                mng.liclass = menu.LiClass;
                mng.icon = menu.Icon;
                mng.subMenus = bll.getsubmainmenus(menu.MenuID, lang.LangID);
                listmenu.Add(mng);
            }

            return PartialView(listmenu);
        }
        public ActionResult getPartnerList()
        {

            CompanyBLL bll = new CompanyBLL();
            LanguageBLL langbll = new LanguageBLL();
            Language lang = langbll.getLangByCode(Request.Cookies["_culture"]["lang"]);
            List<CompanyModels> list = new List<CompanyModels>();

            list = bll.getPartnerList(lang.LangID);

            return PartialView(list);
        }
        public ActionResult getslidewinpos1()
        {
            SlideWinBLL slbll = new SlideWinBLL();
            LanguageBLL langbll = new LanguageBLL();
            Language lang = langbll.getLangByCode(Request.Cookies["_culture"]["lang"]);
            List<SlideWinModels> list = new List<SlideWinModels>();
            list = slbll.getmenubytypeid(1, lang.LangID);
            return PartialView(list);
        }
        public ActionResult getslidewinpos2()
        {
            SlideWinBLL slbll = new SlideWinBLL();
            LanguageBLL langbll = new LanguageBLL();
            Language lang = langbll.getLangByCode(Request.Cookies["_culture"]["lang"]);
            List<SlideWinModels> list = new List<SlideWinModels>();
            list = slbll.getmenubytypeid(2, lang.LangID);
            return PartialView(list);
        }
        public ActionResult getslidewinpos3()
        {
            SlideWinBLL slbll = new SlideWinBLL();
            LanguageBLL langbll = new LanguageBLL();
            Language lang = langbll.getLangByCode(Request.Cookies["_culture"]["lang"]);
            List<SlideWinModels> list = new List<SlideWinModels>();
            list = slbll.getmenubytypeid(3, lang.LangID);
            return PartialView(list);
        }
        public ActionResult getslidewinpos4()
        {
            SlideWinBLL slbll = new SlideWinBLL();
            LanguageBLL langbll = new LanguageBLL();
            Language lang = langbll.getLangByCode(Request.Cookies["_culture"]["lang"]);
            List<SlideWinModels> list = new List<SlideWinModels>();
            list = slbll.getmenubytypeid(4, lang.LangID);
            return PartialView(list);
        }
        public ActionResult getslidewinpos5()
        {
            SlideWinBLL slbll = new SlideWinBLL();
            LanguageBLL langbll = new LanguageBLL();
            Language lang = langbll.getLangByCode(Request.Cookies["_culture"]["lang"]);
            List<SlideWinModels> list = new List<SlideWinModels>();
            list = slbll.getmenubytypeid(5, lang.LangID);
            return PartialView(list);
        }
        public ActionResult getslidewinpos6()
        {
            SlideWinBLL slbll = new SlideWinBLL();
            LanguageBLL langbll = new LanguageBLL();
            Language lang = langbll.getLangByCode(Request.Cookies["_culture"]["lang"]);
            List<SlideWinModels> list = new List<SlideWinModels>();
            list = slbll.getmenubytypeid(6, lang.LangID);
            return PartialView(list);
        }
        public ActionResult getslidewinpos7()
        {
            SlideWinBLL slbll = new SlideWinBLL();
            LanguageBLL langbll = new LanguageBLL();
            Language lang = langbll.getLangByCode(Request.Cookies["_culture"]["lang"]);
            List<SlideWinModels> list = new List<SlideWinModels>();
            list = slbll.getmenubytypeid(7, lang.LangID);
            return PartialView(list);
        }
        public ActionResult getslidewinpos8()
        {
            SlideWinBLL slbll = new SlideWinBLL();
            LanguageBLL langbll = new LanguageBLL();
            Language lang = langbll.getLangByCode(Request.Cookies["_culture"]["lang"]);
            List<SlideWinModels> list = new List<SlideWinModels>();
            list = slbll.getmenubytypeid(8, lang.LangID);
            return PartialView(list);
        }
        public ActionResult getslidewinpos9()
        {
            SlideWinBLL slbll = new SlideWinBLL();
            LanguageBLL langbll = new LanguageBLL();
            Language lang = langbll.getLangByCode(Request.Cookies["_culture"]["lang"]);
            List<SlideWinModels> list = new List<SlideWinModels>();
            list = slbll.getmenubytypeid(9, lang.LangID);
            return PartialView(list);
        }
        public ActionResult getslidewinpos10()
        {
            SlideWinBLL slbll = new SlideWinBLL();
            LanguageBLL langbll = new LanguageBLL();
            Language lang = langbll.getLangByCode(Request.Cookies["_culture"]["lang"]);
            List<SlideWinModels> list = new List<SlideWinModels>();
            list = slbll.getmenubytypeid(10, lang.LangID);
            return PartialView(list);
        }

        public ActionResult gettop5highlight()
        {
            PHighLightBLL hlbll = new PHighLightBLL();
            LanguageBLL langbll = new LanguageBLL();
            Language lang = langbll.getLangByCode(Request.Cookies["_culture"]["lang"]);
            List<PHighLightModels> list = new List<PHighLightModels>();
            list = hlbll.getTop5(lang.LangID);
            return PartialView(list);
        }

       [HttpPost]
        public ActionResult SendEmail(string EmailSendTo, string Name)
        {
            try
            {
                LanguageBLL langbll = new LanguageBLL();
                Language lang = langbll.getLangByCode(Request.Cookies["_culture"]["lang"]);
                string MailFromUserName = ConfigurationManager.AppSettings["MailFromUserName"].ToString();
                string MailFromPassword = ConfigurationManager.AppSettings["MailFromPassword"].ToString();
                string MailSMTP = ConfigurationManager.AppSettings["MailSMTP"].ToString();
                int MailPort = int.Parse(System.Configuration.ConfigurationManager.AppSettings["MailPort"].ToString());

                if (lang.LangID == 1)
                {
                    string imagePath = Server.MapPath(@"/Content/images/phone-icon.png");
                    string imagePath1 = Server.MapPath(@"/Content/Upload/Images/vnbanknet-banner.png");
                    string imagePath2 = Server.MapPath(@"/Content/Upload/Images/card.png");
                    string imagePath3 = Server.MapPath(@"/Content/Upload/Images/honeloan-email.png");
                    string imagePath4 = Server.MapPath(@"/Content/Upload/Images/carloan-email.png");
                    string imagePath5 = Server.MapPath(@"/Content/Upload/Images/internetbanking-email.png");
                    string imagePath6 = Server.MapPath(@"/Content/Upload/Images/mobilebanking-email.png");
                    Models.EmailSend email = new Models.EmailSend();
                    email.EmailSendTo = EmailSendTo;
                    email.Subject = "Cảm ơn bạn đã sử dụng dịch vụ của VNBankNet";
                    email.Body = System.IO.File.ReadAllText(Server.MapPath(@"~/Content/Email.html"));

                    LinkedResource imagelink = new LinkedResource(imagePath);
                    LinkedResource imagelink1 = new LinkedResource(imagePath1);
                    LinkedResource imagelink2 = new LinkedResource(imagePath2);
                    LinkedResource imagelink3 = new LinkedResource(imagePath3);
                    LinkedResource imagelink4 = new LinkedResource(imagePath4);
                    LinkedResource imagelink5 = new LinkedResource(imagePath5);
                    LinkedResource imagelink6 = new LinkedResource(imagePath6);
                    imagelink.ContentId = "imageId";
                    imagelink1.ContentId = "bannerid";
                    imagelink2.ContentId = "cardid";
                    imagelink3.ContentId = "homeloanid";
                    imagelink4.ContentId = "carloanid";
                    imagelink5.ContentId = "internetbankingid";
                    imagelink6.ContentId = "mobileid";
                    AlternateView htmlView = AlternateView.CreateAlternateViewFromString(email.Body, null, MediaTypeNames.Text.Html);
                    htmlView.LinkedResources.Add(imagelink);
                    htmlView.LinkedResources.Add(imagelink1);
                    htmlView.LinkedResources.Add(imagelink2);
                    htmlView.LinkedResources.Add(imagelink3);
                    htmlView.LinkedResources.Add(imagelink4);
                    htmlView.LinkedResources.Add(imagelink5);
                    htmlView.LinkedResources.Add(imagelink6);
                    MailMessage message = new MailMessage();

                    message.AlternateViews.Add(htmlView);
                    message.IsBodyHtml = true;
                    message.From = new System.Net.Mail.MailAddress(MailFromUserName);
                    message.To.Add(email.EmailSendTo);
                    message.Subject = email.Subject;


                    System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient(MailSMTP);// you need an smtp server address to send emails
                    smtp.Credentials = new System.Net.NetworkCredential(MailFromUserName, MailFromPassword); //missing line from ur code
                    smtp.Port = 587;  //missing line from your code
                    smtp.EnableSsl = true;   //missing line from your code
                  //  smtp.UseDefaultCredentials = true;
                    smtp.Send(message);
                    int result = 1;
                    return Json(result, JsonRequestBehavior.AllowGet);
                }
                else
                {
                    string imagePath = Server.MapPath(@"/Content/images/phone-icon.png");
                    string imagePath1 = Server.MapPath(@"/Content/Upload/Images/vnbanknet-banner.png");
                    string imagePath2 = Server.MapPath(@"/Content/Upload/Images/card.png");
                    string imagePath3 = Server.MapPath(@"/Content/Upload/Images/honeloan-email.png");
                    string imagePath4 = Server.MapPath(@"/Content/Upload/Images/carloan-email.png");
                    string imagePath5 = Server.MapPath(@"/Content/Upload/Images/internetbanking-email.png");
                    string imagePath6 = Server.MapPath(@"/Content/Upload/Images/mobilebanking-email.png");
                    Models.EmailSend email = new Models.EmailSend();
                    email.EmailSendTo = EmailSendTo;
                    email.Subject = "Thank you for using the services of VNBankNet";
                    email.Body = System.IO.File.ReadAllText(Server.MapPath(@"~/Content/Email_EN.html"));

                    LinkedResource imagelink = new LinkedResource(imagePath);
                    LinkedResource imagelink1 = new LinkedResource(imagePath1);
                    LinkedResource imagelink2 = new LinkedResource(imagePath2);
                    LinkedResource imagelink3 = new LinkedResource(imagePath3);
                    LinkedResource imagelink4 = new LinkedResource(imagePath4);
                    LinkedResource imagelink5 = new LinkedResource(imagePath5);
                    LinkedResource imagelink6 = new LinkedResource(imagePath6);
                    imagelink.ContentId = "imageId";
                    imagelink1.ContentId = "bannerid";
                    imagelink2.ContentId = "cardid";
                    imagelink3.ContentId = "homeloanid";
                    imagelink4.ContentId = "carloanid";
                    imagelink5.ContentId = "internetbankingid";
                    imagelink6.ContentId = "mobileid";
                    AlternateView htmlView = AlternateView.CreateAlternateViewFromString(email.Body, null, MediaTypeNames.Text.Html);
                    htmlView.LinkedResources.Add(imagelink);
                    htmlView.LinkedResources.Add(imagelink1);
                    htmlView.LinkedResources.Add(imagelink2);
                    htmlView.LinkedResources.Add(imagelink3);
                    htmlView.LinkedResources.Add(imagelink4);
                    htmlView.LinkedResources.Add(imagelink5);
                    htmlView.LinkedResources.Add(imagelink6);
                    MailMessage message = new MailMessage();

                    message.AlternateViews.Add(htmlView);
                    message.IsBodyHtml = true;
                    message.From = new System.Net.Mail.MailAddress(MailFromUserName);
                    message.To.Add(email.EmailSendTo);
                    message.Subject = email.Subject;


                    System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient(MailSMTP);// you need an smtp server address to send emails
                    smtp.Credentials = new System.Net.NetworkCredential(MailFromUserName, MailFromPassword); //missing line from ur code
                    smtp.Port = 587;  //missing line from your code
                    smtp.EnableSsl = true;   //missing line from your code
                    //smtp.UseDefaultCredentials = true;
                    smtp.Send(message);
                    int result = 1;
                    return Json(result, JsonRequestBehavior.AllowGet);
                }
             
            }
            catch (Exception ex)
            {
                Exception ex2 = ex;
                string errorMessage = string.Empty;
                while (ex2 != null)
                {
                    errorMessage += ex2.ToString();
                    ex2 = ex2.InnerException;
                }
              
                int result = -1;
                return Json(errorMessage, JsonRequestBehavior.AllowGet);
            }
        }

       

    }
}
